package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.entity.TbAssertOrg;
import com.southwind.entity.TbAsset;
import com.southwind.entity.TbAssetRole;
import com.southwind.entity.TbTaskAttack;
import com.southwind.form.SearchForm;
import com.southwind.mapper.TbAssertOrgMapper;
import com.southwind.mapper.TbAssetMapper;

import com.southwind.mapper.TbAssetRoleMapper;
import com.southwind.service.TbAssetRoleService;
import com.southwind.service.TbAssetService;
import com.southwind.util.CommonUtil;
import com.southwind.vo.TbAssetVO;
import com.southwind.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fjh
 * @since 2024-01-09
 */
@Service
public class TbAssetServiceImpl extends ServiceImpl<TbAssetMapper, TbAsset> implements TbAssetService {
   @Autowired
   private TbAssetMapper tbAssetMapper;

    @Autowired
    private TbAssertOrgMapper tbAssertOrgMapper;
@Autowired
private TbAssetRoleMapper tbAssetRoleMapper;

    @Override
    public Boolean saveAsset(TbAsset tbAsset) {
        tbAsset.setRegisterTime(CommonUtil.createDate());
        int insert = this.tbAssetMapper.insert(tbAsset);
        return insert == 1;
    }

   @Override
    public PageVO list(Integer page, Integer size) {
        Page<TbAsset> tbAssetPage = new Page<>(page, size);
        Page<TbAsset> resultPage = this.tbAssetMapper.selectPage(tbAssetPage, null);

       List<TbAssetVO> tbAssetVOList = new ArrayList<>();
       for (TbAsset tbAsset : resultPage.getRecords()) {
           TbAssetVO tbAssetVO = new TbAssetVO();
           BeanUtils.copyProperties(tbAsset, tbAssetVO);
           TbAssertOrg tbAssertOrg = this.tbAssertOrgMapper.selectById(tbAsset.getBelongOrg());
           tbAssetVO.setOrgName(tbAssertOrg != null ? tbAssertOrg.getOrgName() : null);

           TbAssetRole tbAssetRole = this.tbAssetRoleMapper.selectById(tbAsset.getAssetUser());
           tbAssetVO.setAssetUser(tbAssetRole != null ? tbAssetRole.getUsername() : null);

           // 设置资产名称
           tbAssetVO.setAssetName(tbAsset.getAssetName());
           tbAssetVOList.add(tbAssetVO);
       }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbAssetVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
   }

    @Override
    public PageVO search(SearchForm searchForm) {
        // 模糊查询+分页
        Page<TbAsset> tbAssetPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<TbAsset> resultPage = null;

        if (searchForm.getValue().equals("")) {
            resultPage = this.tbAssetMapper.selectPage(tbAssetPage, null);
        } else {
            QueryWrapper<TbAsset> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.tbAssetMapper.selectPage(tbAssetPage, queryWrapper);
        }

        List<TbAssetVO> tbAssetVOList = new ArrayList<>();
        for (TbAsset tbAsset : resultPage.getRecords()) {
            TbAssetVO tbAssetVO = new TbAssetVO();
            BeanUtils.copyProperties(tbAsset, tbAssetVO);
            TbAssertOrg tbAssertOrg = this.tbAssertOrgMapper.selectById(tbAsset.getBelongOrg());
            tbAssetVO.setOrgName(tbAssertOrg != null ? tbAssertOrg.getOrgName() : null);

            TbAssetRole tbAssetRole = this.tbAssetRoleMapper.selectById(tbAsset.getAssetUser());
            tbAssetVO.setAssetUser(tbAssetRole != null ? tbAssetRole.getUsername() : null);

            // 设置资产名称
            tbAssetVO.setAssetName(tbAsset.getAssetName());
            tbAssetVOList.add(tbAssetVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbAssetVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }
}
